
import fitz
import os
import re

'''
# 将PDF转化为图片
pdfPath pdf文件的路径
imgPath 图像要保存的文件夹
zoom_x x方向的缩放系数
zoom_y y方向的缩放系数
rotation_angle 旋转角度
'''


def pdf_image(pdfPath, imgPath, zoom_x, zoom_y, rotation_angle):
    # 打开PDF文件
    pdf = fitz.open(pdfPath)
    # 逐页读取PDF
    for pg in range(0, pdf.pageCount):
        page = pdf[pg]
        # 设置缩放和旋转系数
        trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotation_angle)
        pm = page.getPixmap(matrix=trans, alpha=False)
        # 开始写图像
        pm.writePNG(imgPath)
    pdf.close()


def run():
    ins_root = r"E:\毕设数据\pictures\avgNDVI"
    outs_root = r"E:\毕设数据\pictures\avgNDVI\jpg"
    ins = [os.path.join(ins_root, x) for x in os.listdir(ins_root) if x.endswith(".pdf")]
    for i in ins:
        out_name = os.path.join(outs_root, os.path.basename(i).replace("pdf", "jpg"))
        pdf_image(i, out_name, 5, 5, 0)
        print(out_name)



if __name__ == '__main__':
    run()

    # pdf_image(r"E:\毕设数据\pictures\avgNDVI\2009-averageWuhanNDVI.pdf", r"E:\毕设数据\pictures\avgNDVI\jpg\\", 5, 5, 0)